package db.tables;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import org.hsqldb.Types;

import db.DBObject;

public class DBLanguageTable extends DBObject {

	public static final String TABLE_NAME = "language";

	public static final String LANGUAGE_ID_COL = TABLE_NAME + "_id",
			LANGUAGE_NAME_COL = TABLE_NAME + "_name",
			LANGUAGE_SHORT_COL = TABLE_NAME+ "_short";

	public DBLanguageTable() throws ClassNotFoundException, SQLException {
		super(
				TABLE_NAME,
				new Object[][] {
						{ LANGUAGE_ID_COL, Types.BIGINT, SQL_IDENTITY_PRIMARY_KEY, "" },
						{ LANGUAGE_NAME_COL, Types.VARCHAR, SQL_NOT_NULL, "" },
						{ LANGUAGE_SHORT_COL, Types.VARCHAR,SQL_NOT_NULL,""}});
	}
	
	protected void insertDefaultValues(){
		String[] columns={LANGUAGE_NAME_COL,LANGUAGE_SHORT_COL};
		
		//insert some languages [may need to be extended]
		insertColumns(columns, new Object[][]{{"Deutsch","de"},{"English","en"},{"Francais","fr"},{"Espanol","es"}});
	}
	
	public HashMap<Long,String> getLanguages(){
		HashMap<Long, String> result=null;
		ResultSet rs=null;
		try {
			//Long type_id = DBMain.getTypeTable().selectLong(DBTypeTable.TYPE_ID_COL, DBTypeTable.TYPE_NAME_COL, type);
			rs=selectAllAsResultSet();
			result=new HashMap<Long, String>();
			while(rs.next()){
				//System.out.println("ID "+rs.getString(1)+" | "+rs.getString(2));
				result.put(rs.getLong(LANGUAGE_ID_COL),rs.getString(LANGUAGE_NAME_COL));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeResult(rs);
		}
		return result;
	}
}
